package com.tancire.web.controller.app;

import com.tancire.common.core.controller.BaseController;
import com.tancire.common.core.domain.AjaxResult;
import com.tancire.common.core.page.TableDataInfo;
import com.tancire.common.utils.SecurityUtils;
import com.tancire.net.domain.PurseInfo;
import com.tancire.net.domain.PurseLog;
import com.tancire.net.domain.PurseOverall;
import com.tancire.net.service.IPurseInfoService;
import com.tancire.net.service.IPurseLogService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 会员钱包Controller
 *
 * @author chenchao
 * @date 2023-11-20
 */
@RestController
@RequestMapping("/net/app/purse")
public class NetPurseInfoAppController extends BaseController {

    @Resource
    private IPurseInfoService purseInfoService;

    @Resource
    private IPurseLogService purseLogService;

    /**
     * 查询会员钱包流水
     */
    @PreAuthorize("@ss.hasPermi('net:user:union')")
    @GetMapping("/flow/list")
    public TableDataInfo list(PurseLog purseLog) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        purseLog.setUserId(userId);
        startPage();
        List<PurseLog> list = purseLogService.selectPurseLogList(purseLog);
        return getDataTable(list);
    }

    /**
     * 查询会员钱包列表
     */
    @PreAuthorize("@ss.hasPermi('net:user:union')")
    @GetMapping("/overall/info")
    public AjaxResult getOverallPurseInfo() {
        PurseOverall res = new PurseOverall();
        Long userId = SecurityUtils.getLoginUser().getUserId();
        // 查询总积分
        PurseInfo purseInfo = this.purseInfoService.selectPurseInfoByUserId(userId);
        if (null != purseInfo && null != purseInfo.getAccount()) {
            res.setTotalPoints(purseInfo.getActivePoints());
        } else {
            res.setTotalPoints(BigDecimal.ZERO);
        }
        // 获取今日获取积分
        BigDecimal todayPoints = purseLogService.selectPurseTodayPoints(userId);
        res.setTodayPoints(null != todayPoints ? todayPoints : BigDecimal.ZERO);
        return AjaxResult.success("查询成功", res);
    }


}
